package com.wiwf.model.security.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wiwf.model.bean.SysUserVoto;
import com.wiwf.model.security.service.SysTokenService;

/**
 * @author kehui
 */
@Controller
@RequestMapping("sys/user")
public class SysUserController {
	
	@Resource
	private SysTokenService tokenService;

	@RequestMapping("/")
	@ResponseBody
	public SysUserVoto pub(HttpServletResponse response, HttpServletRequest request) {
		return tokenService.getTokenUser(response, request);
	}

	@RequestMapping("/logout")
	@ResponseBody
	public String logout(HttpServletResponse response, HttpServletRequest request) {
		tokenService.delTokenUser(request);
		return "ok";
	}
	
	
	// --------------------------- 拥有某角色
	
	@PreAuthorize("hasAnyRole('visitor')")
	@RequestMapping("/visitor")
	@ResponseBody
	public String userApi() {
		return "你好 , this is visitor api";
	}
	
	@PreAuthorize("hasAnyRole('root')")
	@RequestMapping("/admin")
	@ResponseBody
	public String userAdminApi() {
		return "你好 , this is admin api";
	}
	
	
	// --------------------------- 拥有某权限
	
	@PreAuthorize("hasAnyAuthority('system')")
	@RequestMapping("/system")
	@ResponseBody
	public String system() {
		return "你可以访问 系统管理";
	}
	
	@PreAuthorize("hasAnyAuthority('form')")
	@RequestMapping("/form")
	@ResponseBody
	public String form() {
		return "你可以访问 表单页面";
	}
	
	
	// --------------------------- 拥有某角色 同时 拥有某权限
	
	@PreAuthorize("hasRole('visitor') and hasAnyAuthority('form')")
	@RequestMapping("/visitor/form")
	@ResponseBody
	public String visitorForm() {
		return "你扮演visitor，同时访问 表单页面";
	}
	
	@PreAuthorize("hasRole('root') and hasAnyAuthority('system')")
	@RequestMapping("/root/system")
	@ResponseBody
	public String rootSystem() {
		return "你扮演root，同时访问 系统管理";
	}
	
}
